/** @odoo-module **/
let make_add_function = () => {

    let id = 1;

    return () => {
        return `virtual_${id++}`;
    }

}

const add_dish_key_id = make_add_function();
const add_number_category_id = make_add_function();
const add_dinner_type_id = make_add_function();
const add_dish_bom_id  = make_add_function();
const add_material_item_bom_unit_ratio_id  = make_add_function();
const add_material_item_id = make_add_function();
const add_net_material_id = make_add_function();


const dinnerTypeCategoryComparator = (d1,d2) => {
    let getNum = (v)=>{
        let num = v == 'dn2'?2:v=='dn3'?3:v=='dn5'?5:v=='dn1'?1:6;
        return num;
    };
    let d1Number = getNum(d1.dinner_type);
    let d2Number = getNum(d2.dinner_type);
    if(d1Number == d2Number){
        d1Number = d1.dt_no;
        d2Number = d2.dt_no;
    }
    return d1Number - d2Number;
};

const formatId = (id) =>{
    id = id+'';
    if(id.trim() == ''){
        return ''
    }else if(id.startsWith('virtual_')){
        return id.trim();
    }else if(!isNaN(Number(id))){
        id = Number(id);
        return id;
    }else{
        return id;
    }
};
const isChineseStr = (str) => {
    return /^[\u4E00-\u9FA5]+$/.test(str)
}
const toObj = (arr,k) => {
    let _Obj = {};
    for(let i = 0; i　< arr.length; i++) {
        _Obj[arr[i][k]] = arr[i];
    }
    return _Obj;
};
const formatPrice = (price) => {
    price = '' + price;
    price = price
        .replace(/\s*/g,"") //去除字符串内所有的空格
        .replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
        .replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
        .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 只能输入两个小数
    // if (price.indexOf('.') < 0 && price != '') {
    //     // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
    //     price = parseFloat(price);
    // }
    // if (price[0] == '0' && price != '') {
    //     // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
    //     price = parseFloat(price);
    // }
    if(price != ''){
        price = Number(price);
    }
    return price;
};
const keepTwoDecimal = (num) =>{
    if(num+''.trim() == ''){
        return ''
    };
    num = Number(num) || 0;
    let result = parseFloat(num);
    result = Math.round(num*100)/100;
    return result;
}

// 数组是否有重复值
const arryIsRepeat = (arr) =>{
    let hash = {};
    for (let i in arr) {
        if (hash[arr[i]]) {
            //hash 哈希
            return true;
        }
        hash[arr[i]] = true;
    }
    return false;
}

const formatDishKeyName = (str) => {
    str = str
        .replace(/\s+/g, "")
        .replace(/\{/g, "")
        .replace(/\}/g, "")
        .replace(/\|/g, "")
        .replace(/\\/g, "")
        .replace(/\//g, "");
    return str
}

export {

    add_dish_key_id,add_number_category_id,add_dinner_type_id,dinnerTypeCategoryComparator,formatId,isChineseStr,toObj,add_dish_bom_id,formatPrice,keepTwoDecimal,add_material_item_bom_unit_ratio_id,add_material_item_id,arryIsRepeat,add_net_material_id,formatDishKeyName

}
